package com.ihighest.app.infox.adapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;

/**
 * 评论的适配器
 * 
 * @author lkx Created at 2015-10-22 下午3:09:46
 * @version 1.0
 */
public class CommentAdapter extends BaseAdapter {
	private JSONArray array;
	private final Context context;
	private final LayoutInflater inflater;

	public CommentAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.array = new JSONArray();
	}

	public CommentAdapter(Context context, JSONArray array) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.array = array;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return array == null ? 0 : array.length();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(int arg0, View contextView, ViewGroup arg2) {
		final ViewHolder holder;
		if (contextView == null) {
			holder = new ViewHolder();
			contextView = inflater.inflate(R.layout.infox_activity_content_comment_item, null);
			holder.commenContent = (TextView) contextView.findViewById(R.id.commentContent);
			holder.userImg = (ImageView) contextView.findViewById(R.id.uesrImg);
			holder.userName = (TextView) contextView.findViewById(R.id.userName);
			holder.commentData = (TextView) contextView.findViewById(R.id.commentData);
			holder.thumpUp = (TextView) contextView.findViewById(R.id.commentThumbUp);
			holder.replyCount = (TextView) contextView.findViewById(R.id.replyCount);
			holder.thumoLayout = (RelativeLayout) contextView.findViewById(R.id.commentThumbUpLayout);
			holder.thumpUpImg = (ImageView) contextView.findViewById(R.id.commentThumbUpImg);
			contextView.setTag(holder);
		} else {
			holder = (ViewHolder) contextView.getTag();
		}

		try {
			final String id = array.getJSONObject(arg0).getString("id");
			holder.userName.setText(array.getJSONObject(arg0).getString("name"));
			holder.commenContent.setText(array.getJSONObject(arg0).getString("content"));
			holder.commentData.setText(AppKit.timeToBeforeStr(array.getJSONObject(arg0).getLong("time")));
			holder.replyCount.setText(array.getJSONObject(arg0).getInt("comments") + "");
			// Drawable drawable =
			// context.getResources().getDrawable(R.drawable.infox_person_no_icon);
			// DisplayImageOptions defaultOptions = new
			// DisplayImageOptions.Builder().showImageForEmptyUri(drawable).showImageOnFail(drawable)
			// .showImageOnLoading(drawable).cacheInMemory(true).cacheOnDisc(true).build();
			// ImageLoader.getInstance().displayImage(array.getJSONObject(arg0).getString("image"),
			// holder.userImg, defaultOptions);
			AppKit.SetUserImg(array.getJSONObject(arg0).getString("image"), holder.userImg);
			final int thumpUpCount = array.getJSONObject(arg0).getInt("praises");
			holder.thumpUp.setText(array.getJSONObject(arg0).getInt("praises") + "");
			holder.replyCount.setText(array.getJSONObject(arg0).getInt("comments") + "");
			// final String uid =
			// array.getJSONObject(arg0).getString("memberId");

			if (array.getJSONObject(arg0).getBoolean("isPraises")) {

				// / 这一步必须要做,否则不会显示.
				holder.thumpUpImg.setImageDrawable(context.getResources().getDrawable(R.drawable.infox_comment_thumbup_press));
				holder.thumpUp.setTag("2");

			} else {
				holder.thumpUp.setTag("1");
				// / 这一步必须要做,否则不会显示.
				holder.thumpUpImg.setImageDrawable(context.getResources().getDrawable(R.drawable.infox_comment_thumbup));
			}
			holder.userImg.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					System.out.println("xiaoqing");
					// Intent intent = new Intent(context,
					// UserDetailActivity.class);
					// intent.putExtra("userId", uid);
					// context.startActivity(intent);

				}
			});
			holder.thumoLayout.setOnClickListener(new OnClickListener() {

				private int inta;

				@Override
				public void onClick(View arg0) {
					String action;

					if (holder.thumpUp.getTag().equals("1")) {
						action = "1";
						holder.thumpUp.setTag("2");
						inta = thumpUpCount + 1;
						holder.thumpUpImg.setImageDrawable(context.getResources().getDrawable(R.drawable.infox_comment_thumbup_press));
						holder.thumpUp.setText(inta + "");
					} else {
						action = "2";
						holder.thumpUp.setTag("1");
						if (inta >= 1) {

							holder.thumpUp.setText(inta - 1 + "");
						} else {
							holder.thumpUp.setText("0");
						}
						holder.thumpUpImg.setImageDrawable(context.getResources().getDrawable(R.drawable.infox_comment_thumbup));
					}
					Api.get("comment/praise", ApiParam.create().addParam("id", id).addParam("type", "1").addParam("action", action),
							new ApiCallback(context, true) {

								@Override
								public void handleSuccess(ApiResult result) {

								}

								@Override
								public void handleFailure(Exception e, String msg) {

								}
							}, context);
				}
			});

		} catch (JSONException e) {
			e.printStackTrace();
		}

		return contextView;
	}

	class ViewHolder {

		ImageView userImg;
		TextView userName;
		TextView commenContent;
		TextView commentData;
		TextView thumpUp, replyCount;
		RelativeLayout thumoLayout;
		ImageView thumpUpImg;
	}

	/**
	 * 替换当前数据源数据
	 * 
	 * @param moreArry
	 * 
	 * @author lkx Created at 2015-10-20 下午8:49:23
	 */
	public void setData(JSONArray jsonArray) {
		array = jsonArray;
		notifyDataSetChanged();
	}

	/**
	 * 向已有数据源中追加数据
	 * 
	 * @param jsonArray
	 * 
	 * @author lkx Created at 2015-10-20 下午8:49:00
	 */
	public void appendData(JSONArray jsonArray) {
		try {
			array = AppKit.joinJSONArray(array, jsonArray);
		} catch (JSONException e) {
		}
		notifyDataSetChanged();
	}

	/**
	 * 顶部追加数据
	 * 
	 * @param jsonArray
	 * 
	 * @author lkx Created at 2015-10-20 下午8:49:00
	 */
	public void addOneData(JSONObject json) {
		try {
			array = array.put(0, json);
		} catch (JSONException e) {
		}
		notifyDataSetChanged();
	}
}
